package cn.edu.sasu.service.Impl;



import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.domain.dto.SysHospitalSaveDTO;
import cn.edu.sasu.domain.entity.SysDoctor;
import cn.edu.sasu.domain.entity.SysHospital;
import cn.edu.sasu.domain.entity.SysUser;
import cn.edu.sasu.mapper.SysDoctorMapper;
import cn.edu.sasu.mapper.SysHospitalMapper;
import cn.edu.sasu.service.SysHospitalService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @Author: Ganxue
 * @CreateTime: 2024-04-27
 * @Description: 医院seriviceImp
 * @Version: 1.0
 */
@Service
public class SysHospitalServiceImp implements SysHospitalService {
    @Resource
    private SysHospitalMapper sysHospitalMapper;
    @Resource
    private SysDoctorMapper sysDoctorMapper;

    @Override
    public LayuiRespResult hospitalList(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysHospital> sysDepts = sysHospitalMapper.hospitalList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = sysHospitalMapper.countHospitals(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize,sysDepts);
    }

    @Override
    public List<SysHospital> getHospitals(Integer deleteFlag) {
        return sysHospitalMapper.getAllhospital(deleteFlag);
    }

    @Override
    public RespResult delSysHospitalByUserId(Integer hospitalId) {
        sysHospitalMapper.updateSysHospitalDeleteFlagByUserId(hospitalId,Constant.DELETE_FLAG_YES);
        List<SysDoctor> sysDoctors=sysDoctorMapper.getHospitalDoctor(hospitalId);
        //创建一个数组
        if (sysDoctors.size()==0){
            return RespResult.success("删除成功！");
        }else{
            List<Integer> dataList = new ArrayList<>();
            for (int i=0;i<sysDoctors.toArray().length;i++){
                //数据尽进行添加
                sysDoctors.get(i).getDoctorId();
                dataList.add(sysDoctors.get(i).getDoctorId());
            }
            sysDoctorMapper.updateSysDoctorDeleteFlagByDoctorIds(dataList,Constant.DELETE_FLAG_YES);
            return RespResult.success("删除成功！");
        }

    }
    @Override
    public RespResult batchdel(Integer[] ids) {
        sysHospitalMapper.updateSysHospitalDeleteFlagByUserIds(Arrays.asList(ids),Constant.DELETE_FLAG_YES);
        List<SysDoctor> sysDoctors=sysDoctorMapper.getHistorysDoctor(Arrays.asList(ids));
        if (sysDoctors.size()==0){
            return RespResult.success("删除成功！");
        }else{
            List<Integer> dataList = new ArrayList<>();
            for (int i=0;i<sysDoctors.toArray().length;i++){
                //数据尽进行添加
                sysDoctors.get(i).getDoctorId();
                dataList.add(sysDoctors.get(i).getDoctorId());
            }
            sysDoctorMapper.updateSysDoctorDeleteFlagByDoctorIds(dataList,Constant.DELETE_FLAG_YES);
            return RespResult.success("删除成功！");
        }

    }

    @Override
    public LayuiRespResult search(Integer page, Integer limit, String hospitalName) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysHospital> sysHospitals = sysHospitalMapper.searchSysHospitalList(Constant.DELETE_FLAG_YES,offset,rows,hospitalName);
        Integer totalSize = sysHospitalMapper.countSearchSysHospital(Constant.DELETE_FLAG_YES,hospitalName);
        return LayuiRespResult.success(totalSize,sysHospitals);
    }

    @Override
    public RespResult save(SysHospitalSaveDTO sysHospitalSaveDTO) {
        SysUser hospital = sysHospitalMapper.findSysHospitalByHospitalName(sysHospitalSaveDTO.getHospitalName());
        if(hospital != null){
            return RespResult.error("该医院已经存在，请重新输入！");
        }
        // 对象拷贝
        SysHospital sysHospital = BeanUtil.copyProperties(sysHospitalSaveDTO,SysHospital.class);
        sysHospitalMapper.save(sysHospital);
        return RespResult.success("添加成功");
    }

    @Override
    public RespResult update(SysHospitalSaveDTO sysHospitalSaveDTO) {
        //进行修改操作
        SysHospital sysHospital = BeanUtil.copyProperties(sysHospitalSaveDTO,SysHospital.class);

        sysHospitalMapper.update(sysHospital);
        return  RespResult.success("修改成功");
    }

    @Override
    public List<SysHospital> getHospitalsById(Integer deleteFlagYes, Integer  hospitalId) {
        return sysHospitalMapper.getAllhospitalById(deleteFlagYes,hospitalId);
    }
}

